﻿using Microsoft.Extensions.DependencyInjection;
using Modules.System.User;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Caching;
using PmSoft.DBContext;
using PmSoft.Entity;
using PmSoft.Repositories;
using PmSoft.Utilities;
using static Modules.Project.ProjectConstants;

namespace Modules.Project.Repositories
{
    [UseDI(ServiceLifetime.Scoped)]
    public class ProjectLockOutRepository : Repository<ProjectLockOut, DefaultDbContext>
    {
        private readonly IApplicationContext applicationContext;
        public ProjectLockOutRepository(DefaultDbContext dbContext, IApplicationContext applicationContext) : base(dbContext, applicationContext)
        {
            this.applicationContext = applicationContext;
        }

        public IPagedList<ProjectLockOut> GetProjectLockOuts(ProjectLockOutQuery query, int pageIndex, int pageSize)
        {
            Sql sql = Sql.Builder;
            sql.Select("project_lock_out.id")
                .From("project_lock_out");
            sql.InnerJoin("project as b ").On("project_lock_out.project_id=b.project_id");
            #region 主管单位数据权限
            if (applicationContext.CurrentUser is IDeptUser deptUser && !deptUser.IsSuperAdministrator())
            {
                sql.InnerJoin("project as prj").On($"project_lock_out.project_id=prj.project_id and prj.is_deleted=0 and prj.industry_type=@0 ", deptUser.IndustryType);
                if (deptUser.Level == AdministrativeLevel.City)
                    sql.Append($" and prj.city_code={deptUser.CityCode}");
                if (deptUser.Level == AdministrativeLevel.Area)
                    sql.Append($" and prj.area_code={deptUser.AreaCode}");
            }
            #endregion
            if (!string.IsNullOrEmpty(query.Keywords))
                sql.Where("(b.name Like @0)", "%" + StringUtility.StripSQLInjection(query.Keywords) + "%");
            if (query.ProjectId.HasValue)
                sql.Where("project_lock_out.project_id=@0", query.ProjectId);
            if (query.Status.HasValue)
                sql.Where("status=@0", query.Status);
            sql.OrderBy("project_lock_out.create_time DESC");
            return GetPagingEntities(pageSize, pageIndex, sql);
        }

        public IEnumerable<ProjectLockOut> GetProjectLockOutsByProjectId(int projectId)
        {
            int areaVersion = EntityCacheService.GetAreaVersion("ProjectId", projectId);
            return GetTopEntities(1000, CachingExpirationType.ObjectCollection, () =>
            {
                return $"ProjectLockOutsByProjectId:{projectId}:{areaVersion}";
            }, () =>
            {
                Sql sql = Sql.Builder
                .Select("id")
                .From("project_lock_out")
                .Where("project_id=@0", projectId);
                sql.OrderBy("create_time DESC");
                return sql;
            });
        }

        public IEnumerable<ProjectLockOut> GetProjectLockOutsByStatus(停复工申请状态 status)
        {
            int areaVersion = EntityCacheService.GetAreaVersion("Status", status);
            return GetTopEntities(1000, CachingExpirationType.ObjectCollection, () =>
            {
                return $"ProjectLockOutsByStatus:{status}:{areaVersion}";
            }, () =>
            {
                Sql sql = Sql.Builder
                .Select("id")
                .From("project_lock_out")
                .Where("status=@0", status);
                sql.OrderBy("create_time DESC");
                return sql;
            });
        }
    }
}